import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_transaction_revocation_info.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_purchased_item.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_transaction_offer_type_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/google_play/get_google_play_purchased_subscription_transaction_time.dart';

abstract class CreateGooglePlayPurchasedSubscriptionTransactionFromApiOne {
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayApiPurchasedSubscription apiPurchasedSubscription,
    required String purchaseToken,
    required String purchaseItemSku,
    required String googlePlayUserId,
    required GooglePlayTransactionRevocationInfo? revocationInfo,
  });
}

class CreateGooglePlayPurchasedSubscriptionTransactionFromApiOneImpl
    implements CreateGooglePlayPurchasedSubscriptionTransactionFromApiOne {
  const CreateGooglePlayPurchasedSubscriptionTransactionFromApiOneImpl({
    required CreateGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne
        createGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne,
    required GetGooglePlayPurchasedSubscriptionTransactionTime
        getGooglePlayPurchasedSubscriptionTransactionTime,
  })  : _createGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne =
            createGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne,
        _getGooglePlayPurchasedSubscriptionTransactionTime =
            getGooglePlayPurchasedSubscriptionTransactionTime;

  final CreateGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne
      _createGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne;
  final GetGooglePlayPurchasedSubscriptionTransactionTime
      _getGooglePlayPurchasedSubscriptionTransactionTime;

  @override
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayApiPurchasedSubscription apiPurchasedSubscription,
    required String purchaseToken,
    required String purchaseItemSku,
    required String googlePlayUserId,
    required GooglePlayTransactionRevocationInfo? revocationInfo,
  }) {
    final offerType = _createGooglePlayPurchasedSubscriptionTransactionOfferTypeFromApiOne(
      apiOfferType: apiPurchasedSubscription.offerType,
    );

    final expirationTime = apiPurchasedSubscription.expirationTime;

    final time = _getGooglePlayPurchasedSubscriptionTransactionTime(
      purchaseItemSku: purchaseItemSku,
      offerType: offerType,
      expirationTime: expirationTime,
    );

    return GooglePlayPurchasedSubscriptionTransaction(
      orderId: apiPurchasedSubscription.orderId,
      purchaseItemSku: purchaseItemSku,
      purchaseToken: purchaseToken,
      googlePlayUserId: googlePlayUserId,
      test: apiPurchasedSubscription.test,
      time: time,
      expirationTime: expirationTime,
      offerType: offerType,
      revocationInfo: revocationInfo,
    );
  }
}
